package com.wardrobe.soa.api.trainmanage;

import com.wardrobe.domain.train_manage.TrainManageShowBean;
import com.wardrobe.soa.common.ApiResult;
import com.wardrobe.soa.utils.CommonConsant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by Administrator on 2018/11/15.
 */
@RestController
@RequestMapping("/api")
public class TrainManageController {
    private static final Logger logger = LoggerFactory.getLogger(TrainManageController.class);

    @Autowired
    private TrainManageService trainManageService;

    @RequestMapping("/getTrainManageList")
    public ApiResult getTrainManageList(){
        try {
            List<TrainManageShowBean> trainManageList = trainManageService.getTrainManageList();
            trainManageList.stream().forEach(trainManageShowBean -> {
                trainManageShowBean.setContent("");
                trainManageShowBean.setCreateTime(trainManageShowBean.getCreateTime().substring(0,trainManageShowBean.getCreateTime().length()-2));
                trainManageShowBean.setUpdateTime(trainManageShowBean.getUpdateTime().substring(0,trainManageShowBean.getUpdateTime().length()-2));
            });
            return new ApiResult(CommonConsant.successCode,CommonConsant.success,CommonConsant.successMsg,trainManageList);
        }catch (Exception e){
            logger.error("getTrainManageList",e);
        }
        return new ApiResult(CommonConsant.serverFailedCode,CommonConsant.failed,CommonConsant.serverFailedMsg,"");
    }

    @RequestMapping("/getTrainManageById")
    public ApiResult getTrainManageById(int id){
        try {
            TrainManageShowBean trainManage = trainManageService.getTrainManageById(id);
            trainManage.setCreateTime(trainManage.getCreateTime().substring(0,trainManage.getCreateTime().length()-2));
            trainManage.setUpdateTime(trainManage.getUpdateTime().substring(0,trainManage.getUpdateTime().length()-2));
            return new ApiResult(CommonConsant.successCode,CommonConsant.success,CommonConsant.successMsg,trainManage);
        }catch (Exception e){
            logger.error("getTrainManageById",e);
        }
        return new ApiResult(CommonConsant.serverFailedCode,CommonConsant.failed,CommonConsant.serverFailedMsg,"");
    }
}
